yjx.directive('mSlider',[function(){
    return{
        restrict:'A',
        scope:{
            sliderFunc:'=sliderFunc'
        },
        link:function($scope,elem,attr){
            var minRange = 50, //最小有效滑动范围
                readyMoved = true,//判断每次滑动开始的标记变量
                isLeft=true,
                touchX;//最初touch X坐标
            /*实现左右滑动效果*/
            elem.on('touchstart',function(e){
                if(readyMoved){
                    //console.log(e);
                    touchX = e.originalEvent.touches[0].clientX;
                    readyMoved=false;
                }
            });
            elem.on('touchmove',function(e){
                //e.preventDefault();
                if(!readyMoved){
                    var releaseAt = e.originalEvent.changedTouches[0].clientX
                    if(releaseAt + minRange < touchX) {
                        readyMoved = true;
                        isLeft=true;
                        $scope.sliderFunc(isLeft)
                        //console.log("向左滑动")
                    } else if (releaseAt - minRange > touchX) {
                        readyMoved = true;
                        isLeft=false;
                        $scope.sliderFunc(isLeft)
                        //console.log("向右滑动")
                    }
                }
            });
            elem.on('touchend',function(e){
                if(!readyMoved){
                    var releaseAt = e.originalEvent.changedTouches[0].clientX
                    if(releaseAt + minRange < touchX) {
                        readyMoved = true;
                        isLeft=true;
                        $scope.sliderFunc(isLeft)
                        //console.log("向左滑动")
                    } else if (releaseAt - minRange > touchX) {
                        readyMoved = true;
                        isLeft=false;
                        $scope.sliderFunc(isLeft)
                        //console.log("向右滑动")
                    }
                }
        })
        }
    }
}])